<template>
    <div>
        <van-nav-bar id="reset" title="我的家庭" left-arrow @click-left="backHandle" fixed z-index="100" />




        <div class="pageCon fuPage">
            <div class="topBox">
                <div class="topImg">
                    <span>我的家庭</span>
                    <span>家庭成员便捷邀请</span>
                    <span>人员一目了然</span>
                </div>
            </div>

            <template v-if="familyList.length != 0">
                <div class="titleBox">家庭邀请码</div>
                <div class="itemMain">
                    <div>
                        <img src="../../assets/img/jiaImg.png" alt="">
                    </div>
                    <div @click="copyHandle">
                        <div>
                            <div>
                                <span class="codeText">{{ parentCode }}</span>
                            </div>
                            <!-- <span class="c9">创建时间：2024-04-07</span> -->
                        </div>
                        <span class="gre">复制邀请码</span>
                    </div>
                </div>
                <div class="titleBox">家庭成员<span>({{ familyNum }}人)</span></div>
                <div class="itemMain" v-for="(item, index) in familyList" :key="index">
                    <div>
                        <img :src="item.userImg" alt="">
                    </div>
                    <div>
                        <div>
                            <div>
                                <span>{{ item.userName || '家人' }}
                                    <span v-if="item.parentId == 0" class="tipBox">(家长)</span>
                                </span>
                            </div>
                            <span class="c9">绑定医生：{{ item.doctorName || '暂无' }}</span>
                            <!-- <div class="c9">加入时间：{{ item.vipTime || '暂无' }}</div> -->
                        </div>
                        <!-- <span class="c9">移除成员</span> -->
                    </div>
                </div>
            </template>

            <div v-else class="noFamily">
                <span>暂未加入家庭</span>
            </div>

            <div class="btnBox">
                <div @click="familyHandle(1)" v-if="isVip == 1 && user.parentCode == null">创建家庭</div>
                <div @click="familyHandle(2)" v-if="user.parentCode == null">加入家庭</div>
            </div>
        </div>



        <van-popup v-model="tipShow" round>
            <div class="duiBox">
                <div>
                    加入家庭
                </div>
                <div class="inputBox">
                    <span>家庭邀请码：</span>
                    <input type="text" v-model="codeNum" placeholder="请输入家庭邀请码">
                </div>
                <div class="duiBtn">
                    <div @click="tipShow = false">取消</div>
                    <div @click="tipConfirm">提交</div>
                </div>
            </div>
        </van-popup>

    </div>

</template>

<script>

import { addUserByFamilyCode, creatCode } from '../../network/family'
import { getUserInfoById } from '../../network/user'
export default {
    name: 'HomeR',
    data() {
        return {
            isLoading: false,
            tipShow: false,
            codeNum: '',
            parentCode: '',
            user: JSON.parse(localStorage.user),
            isVip: localStorage.isVip,
            familyList: [],
            familyNum: 0
        }
    },
    mounted() {

    },
    methods: {

        familyHandle(flag) {
            if (flag == 1) {
                // 创建家庭
                creatCode({
                    userId: localStorage.userId
                }).then(res => {
                    if (res.flag) {
                        this.user.parentCode = res.data
                        localStorage.user = JSON.stringify(this.user)
                        this.parentCode = res.data
                        console.log(res.data)
                        this.$toast('已成功创建家庭')
                    }
                })
            } else if (flag == 2) {
                // 加入家庭
                this.tipShow = true

            }
        },

        tipConfirm() {
            if (this.codeNum == '') {
                this.$toast('请填写家庭邀请码')
                return
            }
            console.log(this.codeNum);
            addUserByFamilyCode({
                userId: localStorage.userId,
                familyCode: this.codeNum,
            }).then(res => {
                if (res.flag) {
                    this.$toast('成功加入家庭')
                    this.tipShow = false
                    console.log(res);
                    this.parentCode = this.codeNum
                    this.user.parentCode = this.codeNum
                    localStorage.user = JSON.stringify(this.user)
                    this.getList()

                } else {
                    this.$toast.fail(res.message)
                }
            })
        },

        backHandle() {
            this.$router.back()
        },

        // 复制
        copyHandle() {
            var str = this.parentCode;
            const save = function (e) {
                e.clipboardData.setData('text/plain', str)
                e.preventDefault() // 阻止默认行为
            }
            const once = {
                once: true
            }
            document.addEventListener('copy', save, once) // 添加一个copy事件,当触发时执行一次,执行完删除
            document.execCommand('copy') // 执行copy方法
            this.$toast('复制成功')

        },
        pullHandle() {

            this.isLoading = true
            this.getList()
        },


        getList() {
            getUserInfoById({
                userId: localStorage.userId
            }).then(res => {
                console.log(res);
                if (res.flag) {
                    this.isLoading = false
                    if (res.data.familyList) {
                        // this.familyNum = res.data.familyNum
                        res.data.familyList = res.data.familyList.sort((a, b) => {
                            return a.parentId - b.parentId
                        })

                        this.familyNum = res.data.familyList.length

                        this.familyList = res.data.familyList

                    }


                    console.log(this.familyList);
                }
            })
        },

    },
    created() {
        this.parentCode = this.user.parentCode
        this.getList()
    },
    //计算属性
    computed: {

    },
    filters: {

    }

}

</script>

<style lang='less' scoped>
.topBox {
    margin-bottom: 108px;
    box-sizing: border-box;
    padding-top: 68px;
    width: 750px;
    height: 260px;
    background: #FFFFFF;
    border-radius: 0 0 120px 120px;
    // overflow: hidden;
}

.topImg {
    box-sizing: border-box;
    padding: 50px 0 0 46px;
    display: flex;
    flex-direction: column;
    margin: 0px auto 0px;
    width: 690px;
    height: 306px;
    background: linear-gradient(111deg, #14A870, #20BB66);
    border-radius: 20px;
    font-family: Source Han Sans CN;
    font-weight: 500;
    font-size: 36px;
    color: #FFFFFF;
    background-image: url(../../assets/img/familyImg.png);
    background-size: 100% 100%;
}

.topImg>span:first-child {
    margin-bottom: 14px;
}

.topImg>span:not(:first-child) {
    margin-bottom: 10px;
    font-size: 24px;
    color: rgba(255, 255, 255, .8);
}

.pageCon {
    padding-bottom: 100px;
    box-sizing: border-box;
    position: relative;
    width: 100%;
    height: 100vh;
    background-color: #EEF1F7;
    overflow-y: scroll;
}


#reset /deep/ .van-nav-bar__left .van-nav-bar__text,
#reset /deep/ .van-nav-bar__left .van-icon {
    color: #333 !important;
}


.titleBox {
    margin-bottom: 32px;
    box-sizing: border-box;
    padding-left: 64px;
    width: 100%;
    font-family: Source Han Sans CN;
    font-weight: bold;
    font-size: 28px;
    color: #333333;
}

.titleBox>span {
    margin-left: 10px;
    font-weight: 400;
    font-size: 24px;
    color: #999;
}


.itemMain {
    display: flex;
    box-sizing: border-box;
    padding: 30px 44px 30px 32px;
    margin: 0 auto 24px;
    width: 690px;
    background: #FFFFFF;
    box-shadow: 0px 2px 6px 0px rgba(51, 51, 51, 0.08);
    border-radius: 20px;
    font-family: Source Han Sans CN;
}

.itemMain>div:first-child {
    display: flex;
    align-items: center;
    justify-content: center;
    // margin-top: 14px;
    margin-right: 30px;
    width: 62px;
    height: 62px;
    background: #F2F2F2;
    border-radius: 20px;
}

.itemMain>div:first-child>img {
    width: 38px;
    height: 38px;
}

.itemMain>div:nth-child(2) {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex: 1;
    font-family: Source Han Sans CN;
    font-weight: 400;
    font-family: Source Han Sans CN;
    font-weight: 400;
    font-size: 20px;
    color: #333;
    line-height: 36px;
    // background: aqua;
}

.itemMain>div:nth-child(2)>div>div>span {
    margin-right: 14px;
    font-weight: bold;
    font-size: 28px;
    color: #333333;
}

.c9 {
    color: #858B90;
}

.codeText {
    font-size: 36px !important;

}

.tipBox {
    color: #20BB66;
}

.gre {
    color: #14A870;
}

.btnBox {
    position: fixed;
    bottom: 26px;
    box-sizing: border-box;
    padding: 0 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    font-family: Source Han Sans CN;
    font-weight: 400;
    font-size: 28px;
    color: #F4FDF8;
}

.btnBox>div {
    display: flex;
    align-items: center;
    justify-content: center;

}

.btnBox>div:first-child {
    width: 200px;
    height: 76px;
    background: linear-gradient(146deg, #14A870, #20BB66);
    box-shadow: 0px 2px 6px 0px rgba(51, 51, 51, 0.16);
    border-radius: 38px;
}

.btnBox>div:last-child {

    width: 434px;
    height: 76px;
    background: linear-gradient(146deg, #14A870, #20BB66);
    box-shadow: 0px 2px 6px 0px rgba(51, 51, 51, 0.16);
    border-radius: 38px;
}

.btnBox>div:nth-child(2) {
    margin-left: 30px;
}



.duiBox {
    position: relative;
    width: 642px;
    // height: 590px;
    background: #FAFAFA;
    border-radius: 20px;
    color: #333333;
    line-height: 64px;
    font-family: Source Han Sans CN;

}

.duiBox>div:first-child {
    padding-top: 24px;
    text-align: center;
    width: 100%;
    font-weight: bold;
    font-size: 34px;

}

.duiBtn {
    // position: absolute;
    // bottom: 0px;
    display: flex;
    align-items: center;
    width: 100%;
    height: 90px;
    border-top: 2px solid #f2f2f2;
}

.duiBtn>div {
    flex: 1;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    font-size: 28px;
}

.duiBtn>div:last-child {
    border-left: 2px solid #f2f2f2;
}

.inputBox {
    box-sizing: border-box;
    padding: 24px 48px;
    display: flex;
    align-items: center;
    width: 100%;
    font-size: 28px;
}

.inputBox>input {
    width: 60%;
    border: none;
    outline: none;
    background: transparent;
}

.noFamily {
    margin-top: 300px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    color: #999;

}
</style>